import { getCatList, getHotlist, getPlayList } from '../../api/services/api'
export default ({
    namespaced: true,
    state: {
        songList: {
            cat: '',
            total: 0,
            playlists: [],
        },
        hotTags: [],
        typeList: [
            {
                title: '',
                tags: []
            }
        ],
        loading: false,
        currentType: {
            tag: '全部',
            order: 'hot',
            limit: 35,
            currentPage: 1
        },
    },
    mutations: {
        changeSongList(state, val) {
            state.songList = { ...val };
            // console.log(state.songList.playlists);
            // state.playlists = val.playlists;
        },
        //改变当前页
        changeCurrentType(state, val) {
            state.currentType.currentPage = val
        },
        //加载动画
        changeLoading(state, val) {
            state.loading = !state.loading;
        },
        //改变热门标签
        changeHotTypeList(state, val) {
            state.hotTags = [...val];
        },
        //改变所有标签的细分
        changeAllTags(state, val) {
            state.typeList = [];
            for (let i in val.categories) {
                let tags = val.sub.filter((item) => item.category == i)
                state.typeList.push({ title: val.categories[i], tags })
            }
            // console.log(state.typeList);
        },
        //选择歌单标签类型
        changeType(state, val) {
            state.currentType.tag = val;
        },
        //改变排序类型
        changeOrder(state, val) {
            state.currentType.order = val;
        }
    },
    actions: {
        async getCatList(context, val) {
            const data = await getCatList();
            // console.log('全部歌单分类', data);
            context.commit('changeAllTags', data);
        },
        async getHotlist(context, val) {
            const data = await getHotlist();
            // console.log('热门歌单分类', data);
            context.commit('changeHotTypeList', data.tags);
        },
        async getPlayList(context, val) {
            context.commit('changeLoading');
            const { tag, order, limit, currentPage } = context.state.currentType;
            const data = await getPlayList(tag, order, limit, currentPage);
            // console.log('分类出的歌单', data);
            if (data.code == 200) {
                context.commit('changeSongList', data);
                context.commit('changeLoading');
            }
        }

    },

})
